package com.ruiysoft.service.logistics.impl;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.ruiysoft.conf.mq.LogisticsMqConfig;
import com.ruiysoft.entity.logistics.ILogisticsConfig;
import com.ruiysoft.entity.logistics.LogisticsShopApiConf;
import com.ruiysoft.service.logistics.ExpressCollerFactory;
import com.ruiysoft.service.logistics.IExpressCaller;
import com.ruiysoft.service.logistics.ILogisticsSubscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component

public class LogisticsSubscribeImpl implements ILogisticsSubscribe {

    private static Logger logger = LoggerFactory.getLogger(LogisticsCallBackImpl.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public void push2MQ(Long shopID,LogisticsShopApiConf logisticsShopApiConf, String expressCom, String expressNO) {
        rabbitTemplate.convertAndSend(LogisticsMqConfig.SUBSCRIBE_EXCHANGE_NAME,LogisticsMqConfig.SUBSCRIBE_ROUTE_KEY,createMQVO(shopID,logisticsShopApiConf,expressCom,expressNO));
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = LogisticsMqConfig.SUBSCRIBE_QUEUE_NAME, durable = "true", autoDelete = "false"),
                    exchange = @Exchange(value = LogisticsMqConfig.SUBSCRIBE_EXCHANGE_NAME, type = ExchangeTypes.DIRECT),
                    key = LogisticsMqConfig.SUBSCRIBE_ROUTE_KEY)
    )
    public void onMessage(Channel channel, Message message) {
        try{
            ILogisticsSubscribe.LogisticsSubscribeMQVO mqvo = JSON.parseObject(message.getBody(),ILogisticsSubscribe.LogisticsSubscribeMQVO.class);

            if(logger.isInfoEnabled()) {
                logger.info("物流远程订阅消息队列,apiSupplier{},ShopID:{},ExpressCom:{},ExpressNO:{}"
                        ,mqvo.getLogisticsShopApiConf().getApiSupplier()
                        ,mqvo.getShopID()
                        ,mqvo.getExpressCom()
                        ,mqvo.getExpressNO());
            }
            IExpressCaller iExpressCaller = ExpressCollerFactory.getInstance().getExpressCaller(mqvo.getLogisticsShopApiConf().getApiSupplier());
            ILogisticsConfig logisticsConfig = iExpressCaller.parseConfig(mqvo.getLogisticsShopApiConf().getApiParameters());
            iExpressCaller.subscribe(mqvo.getShopID(), logisticsConfig, mqvo.getExpressCom(), mqvo.getExpressNO());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e){
            logger.error("队列消费出错：物流回调队列",e);
        }
    }

}
